package com.kamloo.stg.data.db;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class StopDAO {
	private SQLiteDatabase m_database;
	private MySQLiteHelper m_dbHelper;
	public static final String TABLE_NAME 		 = "stop";
	public static final String COLUMN_ID 		 = "_id";
	public static final String COLUMN_NAME = "name";
	private String[] m_allColumns = { COLUMN_ID, COLUMN_NAME };
	
	public static final String TABLE_CREATE = 
			"CREATE TABLE " + 
		    TABLE_NAME + "(" + 
		    COLUMN_ID + " INTEGER PRIMARY KEY, " +  
		    COLUMN_NAME	+ " TEXT NOT NULL);";
	
	public StopDAO(Context context) {
		m_dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		m_database = m_dbHelper.getWritableDatabase();
	}

	public void close() {
		m_dbHelper.close();
	}

	
	public void add(Stop stop) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_ID, stop.getId());
		values.put(COLUMN_NAME, stop.getName());
		m_database.insert(TABLE_NAME, null, values);
	}
	
	public void delete(int id) {
		System.out.println("Stop deleted with id: " + id);
		m_database.delete(TABLE_NAME, COLUMN_ID + " = " + id, null);
	}
	
	public Stop select(int id) {
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, COLUMN_ID + " = " + id, 
									  null, null, null, null);
		
		if (cur != null)
			cur.moveToFirst();
		
		Stop newStop = cursorToStop(cur);
		cur.close();
		return newStop;
	}
	
	public ArrayList<Stop> selectAll(){
		ArrayList<Stop> stops = new ArrayList<Stop>();
		
		Cursor cur = m_database.query(TABLE_NAME, m_allColumns, 
				  					  null, null, null, null, null);

		if (cur != null) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				stops.add(cursorToStop(cur));
				cur.moveToNext();
			}
		}

		cur.close();
		return stops;
	}
	
	public void update(Stop stop) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_NAME, stop.getName());
		m_database.update(TABLE_NAME, values, COLUMN_ID + " = " + stop.getId(), null);
	}	
	
	private Stop cursorToStop(Cursor cursor) {
		return new Stop(cursor.getInt(0),
						cursor.getString(1));
	}
	
	
	/*
	public static List<Stop> selectNearest(double minLat, double maxLat, double minLng, double maxLng, SQLiteDatabase db) {
		List<Stop> nearest = new ArrayList<Stop>();
		
		String clause = COL_LAT + ">" + minLat + " AND " + COL_LAT + "<" + maxLat + " AND " + COL_LONG + ">" + minLng + " AND " + COL_LONG + "<" + maxLng;
		
		Cursor cur = db.query(TABLE_NAME, new String[]{ COL_ID, COL_NOM, COL_LAT, COL_LONG }, clause, null, null, null, null);

		// Deuxieme prise :) Code identique @ L40
		if (cur != null) {
			while (cur.moveToNext()){
				
				GPS gps = new GPS(cur.getFloat(cur.getColumnIndex(COL_LAT)),
								  cur.getFloat(cur.getColumnIndex(COL_LONG)));
				
				nearest.add(new Stop(cur.getInt(cur.getColumnIndex(COL_ID)),
						  cur.getString(cur.getColumnIndex(COL_NOM)), "", gps));	
			}
		}
		
		return nearest;
	}
	
	public static Stop selectFromHoraire(int id_horaire, SQLiteDatabase db) {
		Stop a = null;
		String query = String.format(SELECT_HORAIRE, COL_ID, COL_NOM, TABLE_NAME, 
				NoeudDAO.TABLE_NAME, COL_ID, NoeudDAO.COL_ID_ARRET,
				HoraireDAO.TABLE_NAME, HoraireDAO.COL_ID_NOEUD, NoeudDAO.COL_ID,
				HoraireDAO.COL_ID);
		
		Cursor cur = db.rawQuery(query, new String[] { String.valueOf(id_horaire) });
		
		if (cur != null && cur.moveToFirst()) {
			a = new Stop(cur.getInt(cur.getColumnIndex(COL_ID)),
						  cur.getString(cur.getColumnIndex(COL_NOM)));
		}
		return a;
	}
	
	public static String insert(Stop a) {
		return String.format(INSERT, TABLE_NAME, COL_ID, COL_NOM, COL_LAT, COL_LONG, a.getId(), a.getNom().replace("'",  "''"), a.getGps().latitude, a.getGps().longitude);
	}
	*/	
}
